package renderer
{
	import components.OperationTitle;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.containers.HBox;
	import mx.containers.VBox;
	import mx.controls.*;
	import mx.formatters.NumberFormatter;
	import mx.managers.PopUpManager;
	import mx.utils.StringUtil;
	
	import util.*;

	public class ChartRenderer extends HBox
	
	
	{
		
		public var op:OperationTitle; 
		public function ChartRenderer()
		{
			super();
		}
		//点击工件名称
			public function over(event:MouseEvent):void{
				op = new OperationTitle();
				var info:String = event.target.parent.label;
				var arr:Array = info.split("||");
				op.planStartTime=arr[0];
				op.planStopTime = arr[1];
				op.actualStartTime = arr[2];
				op.actualStopTime = arr[3];
				op.x =  event.stageX;
                op.y = event.stageY;
				PopUpManager.addPopUp(op,this,false);
//				PopUpManager.centerPopUp(op);	
			}
			
			//点击工件名称
			public function out(event:Event):void{
				PopUpManager.removePopUp(op);
			}
		override public function set data(value:Object):void 
 
        {
            if ( value != null)
				{
					var _row:XML = XML(value);
					var i:int = 0;
					for each (var prop:XML in _row.elements())
					{
						//每道工序用一个VBox容器						
						var bo:VBox = new VBox();
						//工序名称也用VBox容器
						var box:VBox = new VBox();
						
						var label:*;
						if(prop.@quantityState==2){
							label = new Button();
							label.label = prop.@operationName;
						}else{
							label = new Label();
							label.text = prop.@operationName;
						}
						
													
						label.id = prop.@operationID;
//						label.height = GanttChartUtils.ITEM_HEIGHT;
						label.setStyle("textAlign","left");
						if(prop.@state=="over"&&prop.@defer==1){
							label.setStyle("fontStyle","italic");
						}	
							
						label.mouseChildren = false;
						label.addEventListener(MouseEvent.ROLL_OVER,over);				
						label.addEventListener(MouseEvent.ROLL_OUT,out);
						label.width = GanttChartUtils.GRID_WIDTH-4;
							
						if(prop.@state=="over"){
							box.setStyle("backgroundColor",GanttChartUtils.COLOR_STATE_OVER);							
						}else if(prop.@state=="working"){
							box.setStyle("backgroundColor",GanttChartUtils.COLOR_STATE_WORKING);
						}else if(prop.@state=="waiting"){
							box.setStyle("backgroundColor",GanttChartUtils.COLOR_STATE_WAITING);
						}else if(prop.@state=="pause"){
							box.setStyle("backgroundColor",GanttChartUtils.COLOR_STATE_PAUSE);
						}
						box.x = i*(GanttChartUtils.GRID_WIDTH+GanttChartUtils.GRID_INTERVAL);
						box.y = 0;
						box.width=GanttChartUtils.GRID_WIDTH;
			       		box.height=GanttChartUtils.ITEM_HEIGHT;	
						if(prop.@quanlityState==0){
							box.setStyle("borderStyle", "solid");
				       		box.setStyle("borderColor", "black");
				       		box.setStyle("cornerRadius",4);
						}else if(prop.@quanlityState==1){
							box.setStyle("borderStyle", "solid");
				       		box.setStyle("borderColor", "black");				       		
				       		box.setStyle("cornerRadius",4);
//				       		box.setStyle("backgroundAlpha",0.5);				       		
				       		box.graphics.lineStyle(3, 0xFF0000, 1);
				       		box.graphics.moveTo(0,GanttChartUtils.ITEM_HEIGHT/2);
				       		box.graphics.lineTo(GanttChartUtils.GRID_WIDTH,GanttChartUtils.ITEM_HEIGHT/2);
						}else{
							box.setStyle("borderStyle", "outset");
							box.setStyle("borderSides", 4);
				       		box.setStyle("borderColor", "red");				       		
				       		box.setStyle("cornerRadius",4);
				       		box.setStyle("backgroundAlpha",0.5);				       		
				       		box.graphics.lineStyle(3, 0xFF0000, 1);
				       		box.graphics.moveTo(0,GanttChartUtils.ITEM_HEIGHT/2);
				       		box.graphics.lineTo(GanttChartUtils.GRID_WIDTH,GanttChartUtils.ITEM_HEIGHT/2);
						}
						
						box.addChild(label);
			       		//
			       		box.label = prop.@planStartTime+"||"+prop.@planStopTime+"||"+prop.@actualStartTime+"||"+prop.@actualStopTime	       		
			       		bo.addChild(box);
			       		
			       		var hb1:HBox = new HBox();
			       		//计划开始时间
						var start:Label = new Label();
						if(StringUtil.isWhitespace(prop.@planStartTime)){
							start.text = "";
						}else{
							start.text = prop.@planStartTime.substring(5,10);
						}													
						start.height=GanttChartUtils.ITEM_HEIGHT;
						start.width = GanttChartUtils.GRID_WIDTH/2;
						hb1.addChild(start);
						//计划工时
						var planHours:Label = new Label();
						if(StringUtil.isWhitespace(prop.@planHours)){
							planHours.text = "";
						}else{
							planHours.text = prop.@planHours;
						}
						planHours.height=GanttChartUtils.ITEM_HEIGHT;
						hb1.addChild(planHours);
						bo.addChild(hb1);	
						
						var hb2:HBox = new HBox();
			       		//计划结束时间
						var stop:Label = new Label();
						if(StringUtil.isWhitespace(prop.@planStopTime)){
							stop.text = "";
						}else{
							stop.text = prop.@planStopTime.substring(5,10);
						}													
						stop.height=GanttChartUtils.ITEM_HEIGHT;
						stop.width = GanttChartUtils.GRID_WIDTH/2;
						hb2.addChild(stop);
						//实际工时
						var actualHours:Label = new Label();
						if(StringUtil.isWhitespace(prop.@actualHours)){
							actualHours.text = "";
						}else{
							actualHours.text = new NumberFormatter().format(prop.@actualHours);
						}
						
						actualHours.height=GanttChartUtils.ITEM_HEIGHT;						
						hb2.addChild(actualHours);
						hb2.horizontalScrollPolicy = "off";
						bo.addChild(hb2);					
						
						//工序容器的宽度和颜色
						bo.width=GanttChartUtils.GRID_WIDTH;
						bo.id = "bo"+i;
						bo.label = "hehe";
												
						this.addChild(bo);							
						i++;
					}

            super.createChildren();
        }
		
	}
}
}